﻿using System;
using System.Text;

using Microsoft.WowAddonStudio.Lua.Model;

namespace Microsoft.WowAddonStudio.Lua.LanguageService.Intellisense
{
    public class DescriptionProvider<TDeclaration> : IDescriptionProvider where TDeclaration : Declaration
    {
        /// <summary>
        /// Gets the description of a declaration.
        /// </summary>
        /// <param name="declaration">The declaration.</param>
        /// <returns>The description of a declaration.</returns>
        string IDescriptionProvider.GetDescription(Declaration declaration)
        {
            if (declaration == null)
                throw new ArgumentNullException("declaration");

            return this.GetDescription((TDeclaration)declaration);
        }

        /// <summary>
        /// Gets the description of a declaration.
        /// </summary>
        /// <param name="declaration">The declaration.</param>
        /// <returns>The description of a declaration.</returns>
        public virtual string GetDescription(TDeclaration declaration)
        {
            string type = GetDeclarationTypeSignature(declaration).ToLower();
            if (String.IsNullOrEmpty(type))
                type = declaration.GetType().Name.ToLower();

            var descriptionBuilder = new StringBuilder();
            descriptionBuilder.AppendFormat("({0}) {1}", type, declaration.GetName());

            var summary = declaration.GetSummary();
            if (summary != null)
            {
                descriptionBuilder.AppendLine();
                descriptionBuilder.Append(summary);
            }

            return descriptionBuilder.ToString();
        }

        protected virtual string GetDeclarationTypeSignature(TDeclaration declaration)
        {
            if (declaration is ApiDeclaration)
                return (declaration as ApiDeclaration).GetTypeSignature();

            return declaration.GetType().Name.ToLower();
        }
    }
}
